<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户表格 -->
      <el-table :data="userlist" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <!-- 姓名 -->
        <el-table-column
          prop="username"
          label="姓名"
          width="180"
        ></el-table-column>
        <!-- 邮箱 -->
        <el-table-column
          prop="email"
          label="邮箱"
          width="180"
        ></el-table-column>
        <!-- 电话 -->
        <el-table-column
          prop="mobile"
          label="电话"
          width="180"
        ></el-table-column>
        <!-- 角色 -->
        <el-table-column
          prop="role_name"
          label="角色"
          width="180"
        ></el-table-column>
        <!-- 状态 -->
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="userStateChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <!-- 修改用户信息 -->
            <el-button
              @click="showEditDialog(scope.row)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <!-- 删除用户信息 -->
            <el-button
              @click="removeUserById(scope.row.index)"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
            <!-- 分配角色 -->
            <el-tooltip
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="setRole(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加用户对话框 -->
    <el-dialog
      title="添加新用户"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username" value="admin1"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" value="123456"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="addForm.email"
            value="1658631614@qq.com"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile" value="15281420826"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role_name">
          <el-input v-model="addForm.role_name" value="打工人"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户对话框 -->
    <el-dialog
      @close="editFormClosed"
      title="修改用户信息"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加角色 -->
    <el-dialog
      title="添加新角色"
      :visible.sync="setRoleDialogVisible"
      width="50%"
    >
      <div>
        <span>当前的用户:{{ userInfo.username }}</span>
        <br /><br />
        <span>当前的角色:{{ userInfo.role_name }}</span>
        <br /><br /><span
          >分配新角色:
          <el-select v-model="selectedRoleId" placeholder="请选择">
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.authName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRoleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRoleDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Users",
  data() {
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if (regEmail.test(value)) {
        return cb();
      }
      cb(new Error("请输入正确的邮箱"));
    };
    var checkMobile = (rule, value, cb) => {
      const refMobile =
        /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if (refMobile.test(value)) {
        return cb();
      }
      cb(new Error("请输入正确的手机号"));
    };
    return {
      userlist: [
        {
          id: 500,
          role_name: "资本家",
          username: "admin",
          createtime: "2017-11-09T20:36:26.000Z",
          email: "tige112@163.com",
          mg_state: true,
          type: 1,
          mobile: 15281420826,
        },
        {
          id: 501,
          role_name: "打工人01",
          username: "linken",
          createtime: "2017-11-09T20:36:26.000Z",
          email: "tige112@163.com",
          mg_state: true,
          type: 1,
          mobile: 15281420826,
        },
        {
          id: 502,
          role_name: "打工人02",
          username: "admin",
          createtime: "2017-11-09T20:36:26.000Z",
          email: "tige112@163.com",
          mg_state: true,
          type: 1,
          mobile: 15281420826,
        },
        {
          id: 503,
          role_name: "打工人03",
          username: "admin",
          createtime: "2017-11-09T20:36:26.000Z",
          email: "tige112@163.com",
          mg_state: true,
          type: 1,
          mobile: 15281420826,
        },
        {
          id: 504,
          role_name: "打工人04",
          username: "admin",
          createtime: "2017-11-09T20:36:26.000Z",
          email: "tige112@163.com",
          mg_state: true,
          type: 1,
          mobile: 15281420826,
        },
        {
          id: 505,
          role_name: "打工人05",
          username: "admin",
          createtime: "2017-11-09T20:36:26.000Z",
          email: "tige112@163.com",
          mg_state: true,
          type: 1,
          mobile: 15281420826,
        },
      ],
      queryInfo: {
        // 当前页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 10,
      },
      total: 2,
      addDialogVisible: false,
      addForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
        role_name: "",
      },
      addFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入电子邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
      },
      // 控制修改用户对话框
      editDialogVisible: false,

      editForm: {
        email: "",
        mobile: "",
      },
      editFormRules: {
        email: [
          { required: true, message: "请输入电子邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
      },
      setRoleDialogVisible: false,

      userInfo: {},
      rolelist: [],
    };
  },
  methods: {
    // 监听pagesize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
    },
    // 监听页码值的改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
    },
    userStateChange(userinfo) {
      this.$message.success("更新用户状态成功");
    },
    getUserList() {
      const res = [
          {
            id: 500,
            role_name: "资本家",
            username: "admin",
            createtime: "2017-11-09T20:36:26.000Z",
            email: "tige112@163.com",
            mg_state: true,
            type: 1,
            mobile: 15281420826,
          },
          {
            id: 501,
            role_name: "打工人01",
            username: "linken",
            createtime: "2017-11-09T20:36:26.000Z",
            email: "tige112@163.com",
            mg_state: true,
            type: 1,
            mobile: 15281420826,
          },
          {
            id: 502,
            role_name: "打工人02",
            username: "admin",
            createtime: "2017-11-09T20:36:26.000Z",
            email: "tige112@163.com",
            mg_state: true,
            type: 1,
            mobile: 15281420826,
          },
          {
            id: 503,
            role_name: "打工人03",
            username: "admin",
            createtime: "2017-11-09T20:36:26.000Z",
            email: "tige112@163.com",
            mg_state: true,
            type: 1,
            mobile: 15281420826,
          },
          {
            id: 504,
            role_name: "打工人04",
            username: "admin",
            createtime: "2017-11-09T20:36:26.000Z",
            email: "tige112@163.com",
            mg_state: true,
            type: 1,
            mobile: 15281420826,
          },
          {
            id: 505,
            role_name: "打工人05",
            username: "admin",
            createtime: "2017-11-09T20:36:26.000Z",
            email: "tige112@163.com",
            mg_state: true,
            type: 1,
            mobile: 15281420826,
          },
        ],
        userlist = res;
    },
    // 弹窗关闭时刷新弹窗
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    editFormClosed() {
      this.$refs.editFormRef.resetFields();
    },

    addUser() {
      this.$refs.addFormRef.validate((valid) => {
        if (!valid) return;
        this.userlist.push(this.addForm);
        this.addForm = {};
      });
      setTimeout(() => {
        this.addDialogVisible = false;
      }, 0);
    },
    showEditDialog(editForm) {
      this.editDialogVisible = true;
      this.editForm = editForm;
    },
    editUserInfo() {
      this.$refs.editFormRef.validate((vaild) => {
        if (!vaild) return;
        this.editDialogVisible = false;
        this.$message.success("修改用户信息成功");
      });
    },
    async removeUserById(index) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
      if (confirmResult == "confirm") {
        this.userlist.splice(index, 1);
      }
      this.$message({
        type: "success",
        message: "删除用户成功",
      });
    },

    // 展示分配角色对话框
    setRole(userinfo) {
      this.setRoleDialogVisible = true;
      this.userInfo = userinfo;
    },
  },
};
</script>

<style scoped>
</style>